import Vue from 'vue'
import VueRouter from 'vue-router'
import IndexView from '../views/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    // redirect:{},
    component: IndexView,
    meta: {
      title: '中安世纪职业测试考核系统'
    }
  },
  {
    path: '/answer',
    name: 'answer',
    meta: {
      title: '中安世纪职业测试考核系统',
      keepAlive: true //此组件需要被缓存
    },
    component: () => import('@/views/answer-page/answer-index.vue')
  },
  {
    path: '/preview',
    name: 'preview',
    meta: {
      title: '试卷预览'
    },
    component: () => import('@/views/answer-page/answer-preview.vue')
  },
  {
    path: '/score',
    name: 'score',
    meta: {
      title: '成绩'
    },
    component: () => import('@/views/answer-page/answer-score.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//2.通过路由前置守卫处理 ---在跳转前就触发
router.beforeEach((to, from, next) => {
  // 参数是固定的3个:
  // to: 要跳转到的位置
  // from: 当前位置
  // next: 放行, 允许跳转
  document.title = to.meta.title
  if (to.path == '/' || from.path == '/answer') {
    localStorage.removeItem('you_selectedAnswer')
    localStorage.removeItem('test-paper')
  }
  next() //通过next()还可以解决相同路径不允许重复跳转的问题
})

export default router
